package base;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Statement;

import bean.Patient;
import bean.User;

public class BaseUser {
	
	public static boolean isLog(Base base, User user) {
		ResultSet rs;
		Statement st;
		try {
			st = (Statement) base.getConnection().createStatement();
			rs = (ResultSet) st.executeQuery("Select * from user");
			
			while(rs.next()) {
				if(user.getLogin().equals(rs.getString("login")) && user.getPassword().equals(rs.getString("password"))) {
					return true;
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}
	
	public static User getUser(Base base, String login){
		ResultSet rs;
		Statement st;
		User med = new User();
		try {
			st = (Statement) base.getConnection().createStatement();
			rs = (ResultSet) st.executeQuery("Select * from user where login='"+login+"'");
			
			while(rs.next()) {
				med.setLogin(rs.getString("login"));
				med.setPrenom(rs.getString("prenom"));
				med.setPassword(rs.getString("password"));
				med.setAdresse(rs.getString("adresse"));
				med.setCodePostal(rs.getInt("codePostal"));
				med.setVille(rs.getString("ville"));
				med.setTelephone(rs.getInt("telephone"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return med;

	}
	
	public static boolean modifier(Base base, User user) {
		boolean res = true;
		String query = "UPDATE user SET ";
		query += "login = ?,prenom = ?,adresse = ?,codePostal = ?,ville = ?,telephone = ? WHERE login = ?";
		PreparedStatement ps = null;
		try {
			ps = (PreparedStatement)base.getConnection().prepareStatement(query);
			ps.setString(1,user.getLogin());
			ps.setString(2, user.getPrenom());
			ps.setString(3, user.getAdresse());
			ps.setInt(4, user.getCodePostal());
			ps.setString(5, user.getVille());
			ps.setInt(6, user.getTelephone());
			ps.setString(7,user.getLogin());

			ps.executeUpdate();
		} catch (SQLException e) {
			System.out.println("BaseUser.modifier : erreur "+e.getMessage());
			res = false;
		}
		try {if (ps != null) ps.close();} catch (Exception e) {}
		return res;
	}

}
